package siftest.facebookandroid;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import sif.siffacebook.*;
import sif.siffacebook.AsyncFacebookRunner.RequestListener;
import sif.siffacebook.Facebook.DialogListener;
import siftest.facebookandroid.Include;

public abstract class Application extends Activity {

	public static void getInterfacesif() {
		Interface.menu_top = Interface.getMenu_Top(Interface.menu_top,
				Include.displayWidth, Include.displayHieght);
		Interface.menu_content = Interface.getMenu_Content(
				Interface.menu_content, Include.displayWidth,
				Include.displayHieght);
		Interface.menu_bottom = Interface.getMenu_Bottom(Interface.menu_bottom,
				Include.displayWidth, Include.displayHieght);
	}
	public void NewsFeed()
	{
		Include.mAsyncRunner.request("me/home", new RequestListener() {
			@Override
			public void onComplete(String response, Object state) {
				Log.d("News Feed", response);
				String json = response;
				try {
					// Facebook Profile JSON data
					JSONObject json_response = new JSONObject(json);

					final String return_json = json_response.toString();
					Log.i("return json News Feed", return_json);

					JSONArray arr = json_response.getJSONArray("data");
					for (int i = 0; i < arr.length(); i++) {
						JSONObject obj = arr.getJSONObject(i);
						Log.i("name", obj.getString("name"));
						Include.friendObjects.add(new Friend(obj
								.getString("id"), obj.getString("name")));
					}

					Include.friends_count = arr.length();
					Log.i("News Feed", "" + Include.friends_count);

					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							
							// Toast.makeText(getApplicationContext(), "Name: "
							// + name + "\nEmail: " + email,
							// Toast.LENGTH_LONG).show();
							Toast.makeText(getApplicationContext(),
									"json: " + return_json, Toast.LENGTH_LONG)
									.show();

						}

					});

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onIOException(IOException e, Object state) {
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
			}
		});
	}
	public void getFriends() {
		Include.mAsyncRunner.request("me/friends", new RequestListener() {
			@Override
			public void onComplete(String response, Object state) {
				Log.d("Friends", response);
				String json = response;
				try {
					// Facebook Profile JSON data
					JSONObject json_response = new JSONObject(json);

					final String return_json = json_response.toString();
					Log.i("return json", return_json);

					JSONArray arr = json_response.getJSONArray("data");
					for (int i = 0; i < arr.length(); i++) {
						JSONObject obj = arr.getJSONObject(i);
						Log.i("name", obj.getString("name"));
						Include.friendObjects.add(new Friend(obj
								.getString("id"), obj.getString("name")));
					}

					Include.friends_count = arr.length();
					Log.i("friends_count", "" + Include.friends_count);

					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							
							// Toast.makeText(getApplicationContext(), "Name: "
							// + name + "\nEmail: " + email,
							// Toast.LENGTH_LONG).show();
							Toast.makeText(getApplicationContext(),
									"json: " + return_json, Toast.LENGTH_LONG)
									.show();

						}

					});

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onIOException(IOException e, Object state) {
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
			}
		});

	}
/**
 *  load field
 */
	public void getBooks() {
		// TODO Auto-generated method stub
		Include.mAsyncRunner.request("me/books", new RequestListener() {
			@Override
			public void onComplete(String response, Object state) {
				Log.d("Books", response);

		
			}

			@Override
			public void onIOException(IOException e, Object state) {
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
			}
		});
	}
	public void getAlbums() {
		Include.mAsyncRunner.request("me/albums", new RequestListener() {
			@Override
			public void onComplete(String response, Object state) {
				Log.d("Albums", response);

		
			}

			@Override
			public void onIOException(IOException e, Object state) {
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
			}
		});
	}

	public static void getMessenger() {
	}

	public void postToWall() {
		// post on user's wall.
		Include.facebook.dialog(this, "feed", new DialogListener() {
			public void onFacebookError(FacebookError e) {
			}

			@Override
			public void onError(DialogError e) {
			}

			@Override
			public void onComplete(Bundle values) {
			}

			@Override
			public void onCancel() {
			}
		});
	}

	public void loginToFacebook() {

		Include.mPrefs = getPreferences(MODE_PRIVATE);
		String access_token = Include.mPrefs.getString("access_token", null);
		long expires = Include.mPrefs.getLong("access_expires", 0);

		if (access_token != null) {
			Include.facebook.setAccessToken(access_token);

			Log.d("FB Sessions", "" + Include.facebook.isSessionValid());
		}

		if (expires != 0) {
			Include.facebook.setAccessExpires(expires);
		}

		if (!Include.facebook.isSessionValid()) {
			Include.facebook.authorize(this, new String[] { "email",
					"publish_stream", "read_stream" }, new DialogListener() {

				@Override
				public void onCancel() {
					// Function to handle cancel event
				}

				@Override
				public void onComplete(Bundle values) {
					// Function to handle complete event
					// Edit Preferences and update facebook acess_token
					SharedPreferences.Editor editor = Include.mPrefs.edit();
					editor.putString("access_token",
							Include.facebook.getAccessToken());
					editor.putLong("access_expires",
							Include.facebook.getAccessExpires());
					editor.commit();

				}

				@Override
				public void onError(DialogError error) {
					// Function to handle error

				}

				@Override
				public void onFacebookError(FacebookError fberror) {
					// Function to handle Facebook errors

				}

			});
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Include.facebook.authorizeCallback(requestCode, resultCode, data);
	}

	public void getProfileInformation() {
		Include.mAsyncRunner.request("me", new RequestListener() {// cai ham nay

					public void onComplete(String response, Object state) {
						Log.d("Profile", response);
						String json = response;// cho nay la du lieu tu api tra
												// ve
						try {
							// Facebook Profile JSON data
							JSONObject profile = new JSONObject(json);
							// ong co the log no ra de coi dinh dang
							Log.i("log ne", profile.toString());
							// getting name of the user
							final String name = profile.getString("name");// lay
																			// no
																			// ra
																			// thoi

							// getting email of the user
							final String email = profile.getString("email");
							final String username = profile
									.getString("username");
							final String shool = profile.getString("shool");
							runOnUiThread(new Runnable() {

								public void run() {
									Toast.makeText(
											getApplicationContext(),
											"Name: " + name + "\nEmail: "
													+ email + "\nusername"
													+ username + "\nshool="
													+ shool, Toast.LENGTH_LONG)
											.show();
								}

							});

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					public void onIOException(IOException e, Object state) {
					}

					public void onFileNotFoundException(
							FileNotFoundException e, Object state) {
					}

					public void onMalformedURLException(
							MalformedURLException e, Object state) {
					}

					public void onFacebookError(FacebookError e, Object state) {
					}
				});
	}
	public void getFeed()
	{
		Include.mAsyncRunner.request("me/feed", new RequestListener() {
			@Override
			public void onComplete(String response, Object state) {
				Log.d("Feeds", response);

		
			}

			@Override
			public void onIOException(IOException e, Object state) {
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
			}
		});
	}
}
